package com.example.donghuihui.androidframe.dialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import com.example.donghuihui.androidframe.R;
/**
 * Loading动画
 */
public class LoadingDialog {
    Context mContext = null;
    MyProgressDialog pBar = null;

    public LoadingDialog(Context context) {
        this.mContext = context;
    }

    public boolean isShowing() {
        return  (pBar != null) && pBar.isShowing();
    }

    public void show(){
        if (pBar != null && pBar.isShowing()) {
            pBar.dismiss();
        }
        pBar = new MyProgressDialog(mContext);
        pBar.setCancelable(true);
        pBar.setCanceledOnTouchOutside(false);
        try {
            pBar.show();
        } catch (Exception E) {
            E.printStackTrace();
        }
    }
    public void dismiss() {
        if (null != pBar && pBar.isShowing()) {
            try {
                pBar.stop();
                pBar.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public void setOnDismissListener(DialogInterface.OnDismissListener listener){
        pBar.setOnDismissListener(listener);
    }

    class MyProgressDialog extends Dialog implements DialogInterface {
        ImageView ivIcon;
        private AnimationDrawable animP;
        private boolean mHasStarted;

        public MyProgressDialog(Context context) {
            super(context, R.style.Dialog);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            View view = inflater.inflate(R.layout.dialog_progress, null);
            ivIcon =  (ImageView)view.findViewById(R.id.iv_progress_icon);
            if (animP == null) {
                ivIcon.setImageResource(R.drawable.loading_anim);
                animP = (AnimationDrawable) ivIcon.getDrawable();
            }
            animP.start();
            setView(view);
            super.onCreate(savedInstanceState);
        }
        public void setView(View view) {
            Window mWindow = getWindow();
            mWindow.requestFeature(Window.FEATURE_NO_TITLE);
            mWindow.setContentView(view);
            // super.setView(view, 0, 0, 0, 0);
        }

        @Override
        public void onStart() {
            super.onStart();
            mHasStarted = true;
        }

        @Override
        protected void onStop() {
            super.onStop();
            mHasStarted = false;
//            if (dtvLoadingTxt != null && dtvLoadingTxt.isPlaying()){
//                dtvLoadingTxt.stop();
//            }
        }
        public void stop(){
            if (animP != null) {
                animP.stop();
                animP = null;
            }
//            if (dtvLoadingTxt != null && dtvLoadingTxt.isPlaying()){
//                dtvLoadingTxt.stop();
//            }
        }
    }
}
